package _mst;

/**
 * 面试题 10.03. 搜索旋转数组
 */
public class T1003 {
    public int search(int[] arr, int target) {
        if (arr[0] == target) return 0;
        int left = 0, right = arr.length - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (arr[mid] == target) {
                while (arr[mid - 1] == target) mid--;
                return mid;
            } else if (arr[mid] > arr[left]) {
                if (arr[left] <= target && target < arr[mid]) right = mid - 1;
                else left = mid + 1;
            } else if (arr[mid] < arr[left]) {
                if (arr[mid] < target && target <= arr[right]) left = mid + 1;
                else right = mid - 1;
            } else left++;
        }
        return -1;
    }
}